/*
 * addon_system.cpp
 *
 *  Created on: 2010-1-7
 *      Author: gavinkwoe
 */

#include "addon_features.h"

#if (__ADDON_TARGET__ == __TARGET_SYMBIAN)

#include "addon_std.h"
#include "addon_mem.h"
#include "addon_objbase.h"
#include "addon_graphics.h"
#include "addon_setting.h"
#include "addon_observer.h"

#include "QGraphic.h"
#include "QTextSetting.h"

//#include <e32cmn.h>
//#include <string.h>

/*************************************************************************
* GUID
*************************************************************************/

ADDON_DEFINED_GUID( IAddonSetting, 1, "EA430620-513B-4d30-A063-6AD15A5F2136" )

/*************************************************************************
* STRUCTURE
*************************************************************************/

struct __struct_addon_Setting_data;
typedef struct __struct_addon_Setting_data		addon_Setting_data_t;
typedef struct __struct_addon_Setting_data *	addon_Setting_data_ptr_t;

/*************************************************************************
* DEFINITION
*************************************************************************/

struct __struct_addon_Setting_data
{
	unsigned long		ref_cnt;		// reference count
	CQTextSetting *		setting_obj;
	ADDON_REF(IObject)	font_obj;
};

/*************************************************************************
* LOCAL FUNCTIONS
*************************************************************************/

unsigned long	__CSetting_Construct( ADDON_REF(IObject) thiz, void * usr_data );
unsigned long	__CSetting_Destruct( ADDON_REF(IObject) thiz );
unsigned long	__CSetting_AddRef( ADDON_REF(IObject) thiz );
unsigned long	__CSetting_Release( ADDON_REF(IObject) thiz );

unsigned long	__CSetting_Attach( ADDON_REF(IObject) thiz, void * handle );
unsigned long	__CSetting_SetDirection( ADDON_REF(IObject) thiz, unsigned long dir );
unsigned long	__CSetting_GetDirection( ADDON_REF(IObject) thiz, unsigned long * dir );
unsigned long	__CSetting_SetFont( ADDON_REF(IObject) thiz, ADDON_REF(IObject) ppobj );
unsigned long	__CSetting_GetFont( ADDON_REF(IObject) thiz, ADDON_PREF(IObject) ppobj );
unsigned long	__CSetting_SetVertSpace( ADDON_REF(IObject) thiz, unsigned long space );
unsigned long	__CSetting_GetVertSpace( ADDON_REF(IObject) thiz, unsigned long * pspace );
// 
// const void * const __coclass_vtbl[] =
// {
// 	// IObject
// 	(void *)__CSetting_Construct,
// 		(void *)__CSetting_Destruct,
// 		(void *)__CSetting_AddRef,
// 		(void *)__CSetting_Release,
// 		
// 		// IAddonSetting
// 		(void *)__CSetting_Attach,
// 		(void *)__CSetting_SetDirection,
// 		(void *)__CSetting_GetDirection,
// 		(void *)__CSetting_SetFont,
// 		(void *)__CSetting_GetFont,
// 		(void *)__CSetting_SetVertSpace,
// 		(void *)__CSetting_GetVertSpace,
// };
// 
// const addon_coclass_desc_t __coclass_desc =
// {
// 	(unsigned char *)__coclass_vtbl,
// 		(unsigned long)sizeof( addon_Setting_data_t )
// };

/*************************************************************************
* IMPLEMENTATION
*************************************************************************/

/*************************************************************************
* DESCRIPTION
*	$$__FUNC_DESC__$$
*************************************************************************/
unsigned long __CSetting_Construct( ADDON_REF(IObject) thiz, void * usr_data )
{
	addon_Setting_data_ptr_t obj_data = (addon_Setting_data_ptr_t)ADDON_GET_DATA( thiz );

	ADDON_UNUSED( usr_data );
	
	if ( NULL == obj_data )
		return ADDON_FAILED;

	obj_data->ref_cnt		= 1;
	obj_data->setting_obj	= NULL;
	obj_data->font_obj		= NULL;

	return ADDON_OK;
}

/*************************************************************************
* DESCRIPTION
*	$$__FUNC_DESC__$$
*************************************************************************/
unsigned long __CSetting_Destruct( ADDON_REF(IObject) thiz )
{
	addon_Setting_data_ptr_t obj_data = (addon_Setting_data_ptr_t)ADDON_GET_DATA( thiz );

	if ( NULL == obj_data )
		return ADDON_FAILED;
	
	OBJECT_SAFE_RELEASE( obj_data->font_obj );
	return ADDON_OK;
}

/*************************************************************************
* DESCRIPTION
*	$$__FUNC_DESC__$$
*************************************************************************/
unsigned long __CSetting_AddRef( ADDON_REF(IObject) thiz )
{
	addon_Setting_data_ptr_t obj_data = (addon_Setting_data_ptr_t)ADDON_GET_DATA( thiz );

	if ( NULL == obj_data )
		return ADDON_FAILED;
	
	obj_data->ref_cnt++;
	return ADDON_OK;
}

/*************************************************************************
* DESCRIPTION
*	$$__FUNC_DESC__$$
*************************************************************************/
unsigned long __CSetting_Release( ADDON_REF(IObject) thiz )
{
	addon_Setting_data_ptr_t obj_data = (addon_Setting_data_ptr_t)ADDON_GET_DATA( thiz );

	if ( NULL == obj_data )
		return ADDON_FAILED;
	
	obj_data->ref_cnt -= 1;

	if ( 0 == obj_data->ref_cnt )
	{
		addon_obj_delete( (void *)thiz );
	}
	
	return ADDON_OK;
}

/*************************************************************************
* DESCRIPTION
*	$$__FUNC_DESC__$$
*************************************************************************/
unsigned long __CSetting_Attach( ADDON_REF(IObject) thiz, void * handle )
{
	addon_Setting_data_ptr_t obj_data = (addon_Setting_data_ptr_t)ADDON_GET_DATA( thiz );
	
	if ( NULL == obj_data )
		return ADDON_FAILED;
	
	if ( NULL == obj_data->font_obj )
	{
		obj_data->font_obj = addon_create_font( NULL );
		if ( NULL == obj_data->font_obj )
			return ADDON_FAILED;
	}

	obj_data->setting_obj = (CQTextSetting *)handle;
	IAddonFont_Attach( obj_data->font_obj, handle );

	return ADDON_OK;
}

/*************************************************************************
* DESCRIPTION
*	$$__FUNC_DESC__$$
*************************************************************************/
unsigned long __CSetting_SetDirection( ADDON_REF(IObject) thiz, unsigned long dir )
{
	addon_Setting_data_ptr_t obj_data = (addon_Setting_data_ptr_t)ADDON_GET_DATA( thiz );
	
	if ( NULL == obj_data )
		return ADDON_FAILED;
	
	return ADDON_NONSUPPORT;
}

/*************************************************************************
* DESCRIPTION
*	$$__FUNC_DESC__$$
*************************************************************************/
unsigned long __CSetting_GetDirection( ADDON_REF(IObject) thiz, unsigned long * dir )
{
	QINT32 direction;
	addon_Setting_data_ptr_t obj_data = (addon_Setting_data_ptr_t)ADDON_GET_DATA( thiz );
	
	if ( NULL == obj_data || NULL == dir || NULL == obj_data->setting_obj )
		return ADDON_FAILED;

	direction = obj_data->setting_obj->GetReadPosition();
	if ( direction == CQTextSetting::EREADRIGHT )
	{
		*dir = DIRECTION_CLOCKWISE_90;
	}
	else if ( direction == CQTextSetting::EREADLEFT )
	{
		*dir = DIRECTION_CLOCKWISE_270;
	}
	else
	{
		*dir = DIRECTION_CLOCKWISE_0;
	}

	return ADDON_OK;
}

/*************************************************************************
* DESCRIPTION
*	$$__FUNC_DESC__$$
*************************************************************************/
unsigned long __CSetting_SetFont( ADDON_REF(IObject) thiz, ADDON_REF(IObject) pobj )
{
	addon_Setting_data_ptr_t obj_data = (addon_Setting_data_ptr_t)ADDON_GET_DATA( thiz );
	
	ADDON_UNUSED( pobj );
	
	if ( NULL == obj_data )
		return ADDON_FAILED;
	
	return ADDON_NONSUPPORT;
}

/*************************************************************************
* DESCRIPTION
*	$$__FUNC_DESC__$$
*************************************************************************/
unsigned long __CSetting_GetFont( ADDON_REF(IObject) thiz, ADDON_PREF(IObject) ppobj )
{
	addon_Setting_data_ptr_t obj_data = (addon_Setting_data_ptr_t)ADDON_GET_DATA( thiz );
	
	if ( NULL == obj_data || NULL == ppobj )
		return ADDON_FAILED;
	
	*ppobj = obj_data->font_obj;
	OBJECT_HOLD(  obj_data->font_obj );
	
	return ADDON_OK;
}

/*************************************************************************
* DESCRIPTION
*	$$__FUNC_DESC__$$
*************************************************************************/
unsigned long __CSetting_SetVertSpace( ADDON_REF(IObject) thiz, unsigned long space )
{
	addon_Setting_data_ptr_t obj_data = (addon_Setting_data_ptr_t)ADDON_GET_DATA( thiz );
	
	ADDON_UNUSED( space );
	
	if ( NULL == obj_data )
		return ADDON_FAILED;
	
	return ADDON_NONSUPPORT;
}

/*************************************************************************
* DESCRIPTION
*	$$__FUNC_DESC__$$
*************************************************************************/
unsigned long __CSetting_GetVertSpace( ADDON_REF(IObject) thiz, unsigned long * pspace )
{
	addon_Setting_data_ptr_t obj_data = (addon_Setting_data_ptr_t)ADDON_GET_DATA( thiz );
	
	if ( NULL == obj_data || NULL == pspace )
		return ADDON_FAILED;

	*pspace = obj_data->setting_obj->GetVerticalSpace();
	return ADDON_OK;
}

/*************************************************************************
* DESCRIPTION
*	$$__FUNC_DESC__$$
*************************************************************************/
ADDON_EXPORT ADDON_REF(IObject) addon_create_setting( void * reserved )
{
	//static const 
		void * __coclass_vtbl[11];// =
	//{
		int i=0;
		// IObject
__coclass_vtbl[i++] = 	(void *)__CSetting_Construct;
__coclass_vtbl[i++] = 	(void *)__CSetting_Destruct;
__coclass_vtbl[i++] = 	(void *)__CSetting_AddRef;
__coclass_vtbl[i++] = 	(void *)__CSetting_Release;

		// IAddonSetting
__coclass_vtbl[i++] = 	(void *)__CSetting_Attach;
__coclass_vtbl[i++] = 	(void *)__CSetting_SetDirection;
__coclass_vtbl[i++] = 	(void *)__CSetting_GetDirection;
__coclass_vtbl[i++] = 	(void *)__CSetting_SetFont;
__coclass_vtbl[i++] = 	(void *)__CSetting_GetFont;
__coclass_vtbl[i++] = 	(void *)__CSetting_SetVertSpace;
__coclass_vtbl[i++] = 	(void *)__CSetting_GetVertSpace;
	//};

// 	static const addon_coclass_desc_t __coclass_desc =
// 	{
// 		(unsigned char *)__coclass_vtbl,
// 		(unsigned long)sizeof( addon_Setting_data_t )
// 	};
		addon_coclass_desc_t __coclass_desc;
		
		__coclass_desc.vtbl_size	= (unsigned long)sizeof(__coclass_vtbl);
		__coclass_desc.vtbl_funcs	= (unsigned char *)__coclass_vtbl;
		__coclass_desc.priv_size	= (unsigned long)sizeof(addon_Setting_data_t);
		

	return ( ADDON_REF(IObject) )addon_obj_new( (addon_coclass_desc_ptr_t)&__coclass_desc, reserved );
}

#else	// #if (__ADDON_TARGET__ == __TARGET_SYMBIAN)

/*************************************************************************
* DESCRIPTION
*	$$__FUNC_DESC__$$
*************************************************************************/
ADDON_EXPORT ADDON_REF(IObject) addon_create_setting( void * reserved )
{
	ADDON_UNUSED( reserved );
	return NULL;
}

#endif	// #if (__ADDON_TARGET__ == __TARGET_SYMBIAN)
